<?
require_once("class_GenericMultiComposit.php");
//require_once("class_GenericSerialComposit.php");
require_once("class_DatabaseName.php");


define("BGWRITER_MIN_VERSION",830);
define("FUNCTION_STAT_MIN_VERSION",840);


/********************************************************************************************************/
/* This class represents one row from any table which hase non composit not autoincremented PRIMARY KEY */
/********************************************************************************************************/

class HostCluster extends GenericMultiComposit {
    protected $prod_sql;
    private $track_counts;
    private $track_functions;
    private $conn_string;

    public function HostCluster($hc_id=false) {
	parent::__construct('host_cluster','id',$hc_id);
	$this->conn_string=false;
	$this->add_composit('database_name','DatabaseName','hc_id','id');
	$this->prod_sql=false;
    }

    public function get_conn_string($hide=false) {
	if (!$this->conn_string) {
	    $this->sql->select_c("SELECT get_conn_string(".$this->id.") AS conn_string");
	    $string=$this->sql->get_row_hash();
	    $this->conn_string=$string['conn_string'];
	}
	if ($hide) {
	    return preg_replace('/password=\S+ /','password=****** ',$this->conn_string);
	}
	return $this->conn_string;
    }

    public function track($param='counts') {
	if ($param =='counts') {
	    return $this->track_counts;
	}
	return $this->track_functions;
    }

    public static function pg_version2int($version) {
	$ret=preg_replace('/\./','',$version);
	return $ret;
    }


    public function populate_bg_stat($time_id) {
	if (!$this->prod_sql) {
	    $this->prod_sql=new SQL($this->get_conn_string());
	}
	$this->prod_sql=new SQL($this->get_conn_string());

	if ($this->pg_version2int($this->get_field('pg_version')) >= BGWRITER_MIN_VERSION) {
	    $track_query="SELECT current_setting('track_counts') AS tc";

	    if ($this->pg_version2int($this->get_field('pg_version')) >= FUNCTION_STAT_MIN_VERSION) {
		$track_query.=",current_setting('track_functions') AS tf";
	    }
	    $this->prod_sql->select_c($track_query);
	    $track_res=$this->prod_sql->get_row_hash();

	    $this->track_counts=($track_res['tc'] == 'on' ? true : false);
	    if (isset($track_res['tf'])) {
		$this->track_functions=($track_res['tf'] == 'none' ? false : true);
	    } else {
		$this->track_functions=false;
	    }
	} else {
// Version 8.2 and lLover not supported
	    $this->track_counts=false;
	    $this->track_functions=false;
	}

	if ($this->track_counts) {
	    $this->prod_sql->select_c("SELECT
    pg_stat_get_bgwriter_timed_checkpoints() AS checkpoints_timed,
    pg_stat_get_bgwriter_requested_checkpoints() AS checkpoints_req,
    pg_stat_get_bgwriter_buf_written_checkpoints() AS buffers_checkpoint,
    pg_stat_get_bgwriter_buf_written_clean() AS buffers_clean,
    pg_stat_get_bgwriter_maxwritten_clean() AS maxwritten_clean,
    pg_stat_get_buf_written_backend() AS buffers_backend,
    pg_stat_get_buf_alloc() AS buffers_alloc");

	    $bgw=$this->prod_sql->get_row_hash();
	    $this->sql->non_select_c("INSERT INTO bgwriter_stat VALUES (".$this->id.",".$time_id.",
    ".$bgw['checkpoints_timed'].",
    ".$bgw['checkpoints_req'].",
    ".$bgw['buffers_checkpoint'].",
    ".$bgw['buffers_clean'].",
    ".$bgw['maxwritten_clean'].",
    ".$bgw['buffers_backend'].",
    ".$bgw['buffers_alloc'].")");
	}
    }


}

?>
